#include <QDebug>
#include <time.h>
#include <QTime>
#include <QPluginLoader>

#include "uniontecsystemdll_global.h"
#include "WorkThreadManage/HmiLocalThreadManage.h"
#include "ui_mainwindow.h"
#include "CommunicationMange/SimpleTcpClient/simpletcpsocketdll_global.h"
#include "Interface/PluginInterface/tecpluginremoteinterface.h"

HmiLocalThread::HmiLocalThread(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){
    ui->setupUi(this);
    setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint);
    ptecPluginInstance = NULL;
    pkukatecPluginInstance = NULL;
    this->MainWindowInit();
}

HmiLocalThread::~HmiLocalThread(){
    this->HmiTimerRelease();
}

short  HmiLocalThread :: MainWindowInit(){
    this->mMessageNumber.ErrorMessageNumber = 0;
    this->mMessageNumber.WarningMessageNumber = 0;
    this->mMessageNumber.NormalMessageNumber = 0;
    this->HmiTimerInit();
    this->HmiTimerStart();
    this->SignalSlotConnect();
    this->ui->progressBar->setValue(100);
    return NOERROR;
}

short HmiLocalThread :: HmiTimerInit(){
    this->HmiFastTimer = new QTimer();
    this->HmiSlowTimer = new QTimer();
    this->RunLed = true;
    this->ui->RunLed->setStyleSheet(("background-color: rgb(50,150,30)"));
    return NOERROR;
}

short HmiLocalThread :: HmiTimerRelease(){
    delete HmiFastTimer;
    delete HmiSlowTimer;
    return NOERROR;
}

short HmiLocalThread :: SignalSlotConnect(){
    connect(HmiFastTimer, SIGNAL(timeout()), this, SLOT(FastTimerUpdate()));
    connect(HmiSlowTimer, SIGNAL(timeout()), this, SLOT(SlowTimerUpdate()));

    connect(ui->MessageDetail, SIGNAL(clicked(bool)), this, SLOT(onbtnclicked_MessageDetail()));
    connect(ui->AdvancedSetting, SIGNAL(clicked(bool)), this, SLOT(onbtnclicked_AdvancedSetting()));
    connect(ui->MessageDone, SIGNAL(clicked()), this, SLOT(onbtnclicked_MessageDone()));
    connect(ui->MessageAllDone, SIGNAL(clicked()), this, SLOT(onbtnclicked_MessageAllDone()));

    return NOERROR;
}

short HmiLocalThread :: HmiTimerStart(){
    HmiFastTimer->start(100);
    HmiSlowTimer->start(1000);
    return NOERROR;
}

void HmiLocalThread :: FastTimerUpdate(){
    if(ptecPluginInstance != NULL){
        if(ptecPluginInstance != NULL){
            if(ptecPluginInstance != NULL){
                ptecPluginInstance->FastTimerLoop();
            }
        }
    }
    if(pkukatecPluginInstance != NULL){
        if(pkukatecPluginInstance != NULL){
            if(pkukatecPluginInstance != NULL){
                pkukatecPluginInstance->FastTimerLoop();
            }
        }
    }
}

void HmiLocalThread :: SlowTimerUpdate(){
    if(RunLed == true){
        this->ui->RunLed->setStyleSheet(("background-color: rgb(50,150,30)"));
        RunLed = false;
    }
    else{
        this->ui->RunLed->setStyleSheet(("background-color: rgb(215,215,215)"));
        RunLed = true;
    }
    this->HmiMessageBoxShow();

    this->ui->MainThreadLoopTime->setText(QString::number(this->pBackTcpThread->GetThreadLoopTime()) + "ms");
    this->ui->KukaThreadLoopTime->setText(QString::number(this->pBackUdpThread->GetThreadLoopTime()) + "ms");
    if(ptecPluginInstance != NULL){
        if(ptecPluginInstance != NULL){
            if(ptecPluginInstance != NULL){
                ptecPluginInstance->SlowTimerLoop();
            }
        }
    }
    if(pkukatecPluginInstance != NULL){
        if(pkukatecPluginInstance != NULL){
            if(pkukatecPluginInstance != NULL){
                pkukatecPluginInstance->SlowTimerLoop();
            }
        }
    }
}

void HmiLocalThread :: onbtnclicked_MessageDetail(){

}
void HmiLocalThread :: onbtnclicked_AdvancedSetting(){

}

short HmiLocalThread :: MainWindowShow(){
    this->show();
    return NOERROR;
}

short HmiLocalThread :: Adminshow(){
    this->show();
    this->ui->Main->setCurrentIndex(1);
    return NOERROR;
}

short HmiLocalThread :: MainWindowHide(){
    this->hide();
    return NOERROR;
}

short HmiLocalThread :: WriteMessageBox(MessageType Type, const QString Data){
    QString AllData;
    QTime time = QTime::currentTime();
    switch (Type) {
    case MessageType::Message:
        AllData = time.toString("hh:mm:ss") + ": <br>" + "&nbsp;&nbsp;&nbsp;&nbsp;" + Data;
        NormalMessage.push_back(AllData);
        mMessageNumber.NormalMessageNumber = mMessageNumber.NormalMessageNumber + 1;
        this->ui->NormalMessageNumber->setNum(mMessageNumber.NormalMessageNumber);
        qDebug()<< "Message:" <<Data;
        break;
    case MessageType::Warring:
        AllData = time.toString("hh:mm:ss") + ": <br>" + "&nbsp;&nbsp;&nbsp;&nbsp;" + Data;
        WarningMessage.push_back(AllData);
        mMessageNumber.WarningMessageNumber = mMessageNumber.WarningMessageNumber + 1;
        this->ui->WarningMessageNumber->setNum(mMessageNumber.WarningMessageNumber);
        qDebug()<< "Warring:" << Data;
        break;
    case MessageType::Error:
        AllData = time.toString("hh:mm:ss") + ": <br>" + "&nbsp;&nbsp;&nbsp;&nbsp;" + Data;
        ErrorMessage.push_back(AllData);
        mMessageNumber.ErrorMessageNumber = mMessageNumber.ErrorMessageNumber + 1;
        this->ui->ErrorMessageNumber->setNum(mMessageNumber.ErrorMessageNumber);
        qDebug()<< "Error:" << Data;
        break;
    default:
        break;
    }
    return NOERROR;
}

void HmiLocalThread :: onbtnclicked_MessageDone(){
    if(ErrorMessage.isEmpty()){
        if(WarningMessage.isEmpty()){
            if(NormalMessage.isEmpty()){
            }
            else{
                NormalMessage.pop_back();
                mMessageNumber.NormalMessageNumber = mMessageNumber.NormalMessageNumber - 1;
                this->ui->NormalMessageNumber->setNum(mMessageNumber.NormalMessageNumber);
            }
        }
        else{
            WarningMessage.pop_back();
            mMessageNumber.WarningMessageNumber = mMessageNumber.WarningMessageNumber - 1;
            this->ui->WarningMessageNumber->setNum(mMessageNumber.WarningMessageNumber);
        }
    }
    else{
        ErrorMessage.pop_back();
        mMessageNumber.ErrorMessageNumber = mMessageNumber.ErrorMessageNumber - 1;
        this->ui->ErrorMessageNumber->setNum(mMessageNumber.ErrorMessageNumber);
    }
}

void HmiLocalThread :: onbtnclicked_MessageAllDone(){
    if(NormalMessage.isEmpty()){
    }
    else{
        NormalMessage.clear();
        mMessageNumber.NormalMessageNumber = 0;
        this->ui->NormalMessageNumber->setNum(mMessageNumber.NormalMessageNumber);
    }
}

short HmiLocalThread ::HmiMessageBoxShow(){
    if(ErrorMessage.isEmpty()){
        if(WarningMessage.isEmpty()){
            if(NormalMessage.isEmpty()){
                this->ui->MessageBox->setText("NULL");
                this->ui->MessageBox->setStyleSheet("QLabel{border:0px solid rgb(0,0,0);}");
            }
            else{
                this->ui->MessageBox->setText(NormalMessage[NormalMessage.size()-1]);
                this->ui->MessageBox->setStyleSheet("QLabel{border:2px solid rgb(0,0,0);}");
            }
        }
        else{
            this->ui->MessageBox->setText(WarningMessage[WarningMessage.size()-1]);
            this->ui->MessageBox->setStyleSheet("QLabel{border:2px solid rgb(255,255,0);}");
        }
    }
    else{
        this->ui->MessageBox->setText(ErrorMessage[ErrorMessage.size()-1]);
        this->ui->MessageBox->setStyleSheet("QLabel{border:2px solid rgb(255,0,0);}");
    }
    return NOERROR;
}

short HmiLocalThread :: WriteProgressBar(const int value){
    this->ui->progressBar->setValue(value);
    return NOERROR;
}

short  HmiLocalThread :: PluginInstanceConnect(tecPluginInterface* p){
    if(this->ptecPluginInstance == nullptr){
        this->ptecPluginInstance = p;
        this->ui->Main->insertWidget(2,ptecPluginInstance->RegisiterPluginUi());
        this->ui->Main->setCurrentIndex(2);
    }
    return NOERROR;
}

short  HmiLocalThread :: KUKAPluginInstanceConnect(kukatecPluginInterface* p){
    if(this->pkukatecPluginInstance == nullptr){
        this->pkukatecPluginInstance = p;
        this->ui->Main->insertWidget(2,pkukatecPluginInstance->RegisiterPluginUi());
        this->ui->Main->setCurrentIndex(2);
    }
    return NOERROR;
}

short  HmiLocalThread :: PluginInstanceLogOut(){
    if(this->ptecPluginInstance != nullptr){
        this->ui->Main->removeWidget(ptecPluginInstance->RegisiterPluginUi());
        ptecPluginInstance = nullptr;
    }
    if(this->pkukatecPluginInstance != nullptr){
        this->ui->Main->removeWidget(pkukatecPluginInstance->RegisiterPluginUi());
        pkukatecPluginInstance = nullptr;
    }
    return NOERROR;
}
